package com.yyjj.hangqing.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.yyjj.hangqing.HqNetUrl;
import com.yyjj.hangqing.R;
import com.yyjj.hangqing.adapter.HqRiseAdapter;
import com.yyjj.hangqing.adapter.viewpager.ViewPagerTitleAdapter;
import com.yyjj.hangqing.entity.HqRiseEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.entity.BaseTResp;

/**
 * 行情涨幅
 */
public class HqRiseFragment extends BaseFragment {
    HqRiseAdapter mAdapter;
    RecyclerView recyclerView_top;
    TabLayout mytab;
//    CustomViewpager viewPager;
    ViewPager viewPager;
    public String[] titles = new String[]{"24H涨幅", "24H跌幅"};
    private List<Fragment> fragmentList = new ArrayList<>();
    @Override
    public int getContentLayout() {
        return R.layout.hq_fragment_tab;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        viewPager=mContentView.findViewById(R.id.viewpager);
        mytab=mContentView.findViewById(R.id.mytab);
        fragmentList.add(ZfbFragment.newInstance(0));
        fragmentList.add(ZfbFragment.newInstance(1));
        viewPager.setAdapter(new ViewPagerTitleAdapter(getChildFragmentManager(), fragmentList, Arrays.asList(titles)));
        viewPager.setOffscreenPageLimit(2);
        mytab.setTabMode(TabLayout.MODE_FIXED);
        mytab.setTabGravity(TabLayout.GRAVITY_FILL);
        mytab.setupWithViewPager(viewPager);
        recyclerView_top = mContentView.findViewById(R.id.recyclerView_top);
        recyclerView_top.setLayoutManager(new GridLayoutManager(mContext, 3));
        mAdapter = new HqRiseAdapter();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("name", mAdapter.getData().get(position).getCoin_full_name());
                bundle.putString("symbol", mAdapter.getData().get(position).getCoin_name().toUpperCase());
                ARouter.getInstance().build("/hq/kline/coin").with(bundle).navigation();
            }
        });
        recyclerView_top.setAdapter(mAdapter);

        getTopData();


//        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int i, float v, int i1) {
//
//            }
//            @Override
//            public void onPageSelected(int position) {
//                viewPager.setCurrentItem(position);
//
//            }
//            @Override
//            public void onPageScrollStateChanged(int i) {
//
//            }
//        });
//        viewPager.setCurrentItem(0);
    }

    public static HqRiseFragment newInstance() {
        Bundle args = new Bundle();
        HqRiseFragment fragment = new HqRiseFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void loadData() {
        getTopData();
    }


    void getTopData() {
        ViseHttp.GET(HqNetUrl.BUBIWANG).tag(HqNetUrl.BUBIWANG).request(new ACallback<BaseTResp<List<HqRiseEntity>>>() {

            @Override
            public void onSuccess(BaseTResp<List<HqRiseEntity>> data) {
                mAdapter.replaceData(data.data.subList(0, 3));
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
    }

}
